// IniFile.cpp: implementation of the CIniReader class.
//
//////////////////////////////////////////////////////////////////////

#include "StdAfx.h"
#include "IniReader.h"
//#include <afxcoll.h>
#include <iostream>
#include <string>
#include <fstream>
//#include <locale.h>
#include <vector>
//#include <bitset>

#include "utf8Reader\utf8.h"

using namespace std;

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif



//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
// default constructor
CIniReader::CIniReader()
{
	//m_sectionList = new CStringList();
	//m_sectionDataList = new CStringList();
}

CIniReader::CIniReader(CString strFile)
{
	m_strFileName = strFile;
	//CStdioFile readfile;
	//CString szText;
	//readfile.Open(_T("Hallo_Welt.txt"),CFile::modeRead|CFile::typeText);

	//while (readfile.ReadString(szText))	; 
	//setlocale (LC_ALL,"");
    //codecvt <wchar_t, char_traits <wchar_t> >;

	//bitset<8> octet;

	//wifstream inp(strFile);
	////inp >> octet;

	//if (!inp) {
	//	cout << "\n" << "Error reading " <<strFile << endl;
	//}
	//wstring s;
	//TCHAR buffer[512];
	//for (; getline(inp,s) ; ) {
	//	//MultiByteToWideChar(CP_UTF8,0,s.c_str(),s.length(),buffer,-1);
	//	//WideCharToMultiByte(CP_UTF8,0,s.c_str(),s.length(),multybyte,255,0,0);
	//	MessageBox(0,buffer,_T("TraceMsg"),MB_OK|MB_ICONINFORMATION);
	//}
	// Open the test file (contains UTF-8 encoded text)
	ifstream fs8(strFile);
	if (!fs8.is_open()) {
		cout << "Could not open " << strFile << endl;
	}

	unsigned line_count = 1;
	string line;
	// Play with all the lines in the file
	while (getline(fs8, line)) {
		// check for invalid utf-8 (for a simple yes/no check, there is also utf8::is_valid function)
		string::iterator end_it = utf8::find_invalid(line.begin(), line.end());
		if (end_it != line.end()) {
			cout << "Invalid UTF-8 encoding detected at line " << line_count << "\n";
			cout << "This part is fine: " << string(line.begin(), end_it) << "\n";
		}

		// Get the line length (at least for the valid part)
		int length = utf8::distance(line.begin(), end_it);
		cout << "Length of line " << line_count << " is " << length <<  "\n";

		// Convert it to utf-16
		vector<unsigned short> utf16line;
		utf8::utf8to16(line.begin(), end_it, back_inserter(utf16line));

		wstring uniStr;
		std::vector<unsigned short>::const_iterator itr;

		for(itr=utf16line.begin(); itr!=utf16line.end(); ++itr){
			uniStr.append(1,*itr);
		}

		if (uniStr.find(_T(';'),0) == 0)
		{
			continue;
		}

		size_t pos = uniStr.find(_T('='));
//CString trace = uniStr.c_str();
//trace.Format(_T("found %d"),pos);
//MessageBox(0,trace,_T("TraceMsg"),MB_OK|MB_ICONINFORMATION);

		if (pos > 0)
		{
			std::wstring keyStd = uniStr.substr (0,pos);
			CString key = keyStd.c_str();
            //std:wstring valueStr = uniStr.substr (pos);
			//keysMap.insert(TStrStrPair(key, valueStr));
MessageBox(0,keyStd.c_str(),_T("TraceMsg"),MB_OK|MB_ICONINFORMATION);
			//uniStr= utf16line;
			//MessageBox(0,(keyStd+valueStr).c_str(),_T("TraceMsg"),MB_OK|MB_ICONINFORMATION);
		}


		line_count++;
	}

	//m_sectionList = new CStringList();
	//m_sectionDataList = new CStringList();
}

CIniReader::~CIniReader()
{
	//delete m_sectionList;
	//delete m_sectionDataList;
}

// Used to retrieve a value give the section and key
CString CIniReader::getKeyValue(CString strKey,CString strSection)
{
	//TCHAR ac_Result[255];

	// Get the info from the .ini file	
	//m_lRetValue = GetPrivateProfileString((LPCTSTR)strSection,(LPCTSTR)strKey,
	//	_T(""),ac_Result, 255, (LPCTSTR)m_strFileName);	
	//std::string key;
	//key=strKey.GetBuffer();

	std::wstring strValue = keysMap[strKey];

	CString strResult;
	strValue = strValue;
	return strResult;
}

// Used to add or set a key value pair to a section
long CIniReader::setKey(CString strValue, CString strKey, CString strSection)
{
	m_lRetValue = WritePrivateProfileString (strSection, strKey, 
		strValue, m_strFileName);

	return m_lRetValue;
}

// Used to find out if a given section exists
BOOL CIniReader::sectionExists(CString strSection)
{
	TCHAR ac_Result[100];	
	CString csAux;
	// Get the info from the .ini file	
	m_lRetValue = GetPrivateProfileString((LPCTSTR)strSection,NULL,
		_T(""),ac_Result, 90, (LPCTSTR)m_strFileName);
	// Return if we could retrieve any info from that section
	return (m_lRetValue > 0);
}

// Used to retrieve all of the  section names in the ini file
//CStringList* CIniReader::getSectionNames()  //returns collection of section names
//{
//	TCHAR ac_Result[2000];
//	m_sectionList->RemoveAll();
//
//	m_lRetValue = GetPrivateProfileSectionNames(ac_Result,2000,(LPCTSTR)m_strFileName);
//
//	CString strSectionName;
//	for(int i=0; i<m_lRetValue; i++)
//	{
//		if(ac_Result[i] != '\0') {
//			strSectionName = strSectionName + ac_Result[i];
//		} else {
//			if(strSectionName != "") {
//				m_sectionList->InsertAfter(m_sectionList->GetTailPosition(),strSectionName);
//			}
//			strSectionName = "";
//		}
//	}
//
//	return m_sectionList;
//}

// Used to retrieve all key/value pairs of a given section.  
//CStringList* CIniReader::getSectionData(CString strSection)  
//{
//	char ac_Result[2000];  //change size depending on needs
//	m_sectionDataList->RemoveAll();
//	m_lRetValue = GetPrivateProfileSection((LPCTSTR)strSection, ac_Result, 2000, (LPCTSTR)m_strFileName);
//
//	CString strSectionData;
//	for(int i=0; i<m_lRetValue; i++)
//	{
//		if(ac_Result[i] != '\0') {
//			strSectionData = strSectionData + ac_Result[i];
//		} else {
//			if(strSectionData != "") {
//				m_sectionDataList->InsertAfter(m_sectionDataList->GetTailPosition(),strSectionData);
//			}
//			strSectionData = "";
//		}
//	}
//
//	return m_sectionDataList;
//}

void CIniReader::setINIFileName(CString strINIFile)
{
	m_strFileName = strINIFile;
}

